import 'dart:convert';

import 'song.dart';

class Playlist {
  final String id;
  String name;
  final List<Song> songs;

  Playlist({required this.id, required this.name, List<Song>? songs})
      : songs = songs ?? <Song>[];

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'songs': songs.map((s) => s.toJson()).toList(),
    };
  }

  static Playlist fromJson(Map<String, dynamic> json) {
    final rawSongs = (json['songs'] as List?) ?? const [];
    return Playlist(
      id: json['id'] as String,
      name: json['name'] as String,
      songs: rawSongs.map((e) => Song.fromJson(e as Map<String, dynamic>)).toList(),
    );
  }

  @override
  String toString() => jsonEncode(toJson());
}